package com.winning.java.list;

import com.winning.java.utils.ExtentionUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.io.Console;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author: rqx
 * @create: 2023/6/5 9:31
 * @description:
 */
@Slf4j
public class Main {
    @Test
    public void ListTest() {
        List<People> list = People.GetList();

        log.info("list:{}", ExtentionUtils.toJson(list));
    }


    @Test
    public void LambdaForEachTest() {
        var list = People.GetList();
        list.stream().forEach(item->item.setAge(item.getAge()+1));

        log.info("list:{}", ExtentionUtils.toJson(list));
    }

    @Test
    public void LambdaWhereTest() {
        var list = People.GetList();

        var result = list.stream().filter(item->item.getAge()>20).collect(Collectors.toList());

        log.info("result:{}", ExtentionUtils.toJson(result));
    }


    @Test
    public void LambdaFindTest() {
        var list = People.GetList();
        var result = list.stream().filter(item->item.getName().contains("张")).findFirst();

        log.info("result:{}", ExtentionUtils.toJson(result));
    }


    @Test
    public void LambdaFindAllTest() {
        var list = People.GetList();
        var result = list.stream().filter(item->item.getName().contains("张")).collect(Collectors.toList());

        log.info("result:{}", ExtentionUtils.toJson(result));
    }

    @Test
    public void LambdaSelectTest() {
        var list = People.GetList();
        var result1 = list.stream().map(item->item.getName()).collect(Collectors.toList());
        var result2 = list.stream().map(People::getName).collect(Collectors.toList());

        log.info("result1:{}", ExtentionUtils.toJson(result1));
        log.info("result2:{}", ExtentionUtils.toJson(result2));
    }

    @Test
    public void LambdaDicTest()
    {
        var list = People.GetList();
        var map = list.stream().collect(Collectors.toMap(People::getName, Function.identity()));

        log.info("map:{}", ExtentionUtils.toJson(map));
    }
}
